package borge.web.teamspirit.client.page.team.panels;

import java.util.HashMap;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.data.Serie;
import borge.web.teamspirit.client.data.Team;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.PanelHandler;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.page.shared.widgets.ListDataWidget;
import borge.web.teamspirit.client.page.shared.widgets.ListPersonsWidget;
import borge.web.teamspirit.client.page.shared.widgets.ListTeamsWidget;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.page.team.widgets.ListMatchesWidget;
import borge.web.teamspirit.client.page.team.widgets.ListPlayersWidget;
import borge.web.teamspirit.client.page.team.widgets.ListSeriesWidget;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.ServerComm;

import com.google.gwt.user.client.Window;

public class ListPanel extends MiddlePanel implements WidgetEventHandler, ServerCallbackHandler {
	
	@SuppressWarnings("unchecked")
	private ListDataWidget iActiveWidget;
	
	private HashMap<String, ListDataWidget> iTokenMap;
	
	public ListPanel(/*ServerComm aServerComm,*/ PanelHandler aHandler){
			
		super(/*aServerComm,*/ aHandler);
		//iCreatePlayerWidget = new CreatePlayerWidget(this, null);
		//iSerieWidget = new ShowSerieWidget(null, this);
		
		//add(iCreatePlayerWidget);
		iTokenMap = new HashMap<String, ListDataWidget>();
		
	}
	
	public void handleWidgetEvent(WidgetEventType aEvent){
		
		switch(aEvent){

		case DELETE_PLAYER: {
			
			TeamPlayer p = (TeamPlayer) iActiveWidget.getDataObject();
			ServerComm.deleteObject(p, this);
			break;
		}
		case DELETE_SERIE: {
		    Serie s = (Serie) iActiveWidget.getDataObject();
		    ServerComm.deleteObject(s, this);
		    break;
		}
		
		case DELETE_MATCH: {
		    Match m = (Match) iActiveWidget.getDataObject();
		    ServerComm.deleteObject(m, this);
		    break;
		}
		case DELETE_TEAM: {
			Team t = (Team) iActiveWidget.getDataObject();
			// delete team
			break;
		}
		}
		
	}
	
	private void setActiveWidget(ListDataWidget aWidget){
		
		if(iActiveWidget != null)
			remove(iActiveWidget);
		
		iActiveWidget = aWidget;
		add(iActiveWidget);
		
	}
	
	
    @SuppressWarnings("unchecked")
    public void handleServerCallback(DBQueryResults aResult, CallbackType aType){

    	//Window.alert("callback = " + aType);
    	
        //remove(0);
    	//Window.alert("callback");
    	if(!aResult.querySucceeded()){
    		Window.alert("failed:" + aResult.getErrorMsg());
    		return;
    	}
    	
    	else{ 
        
			switch (aType) {
			case RETRIEVED_DATA: {

				//Window.alert("retrieved");
				if (!aResult.querySucceeded())
					Window.alert("failed");

				if (aResult.getNumResults() == 0)
					Window.alert("list is null");

				iActiveWidget.dataUpdate(aResult);
				
				break;
			}
			case DELETED: {

				iActiveWidget.objectDeleted();
				break;

			}
			}
    	}

    }
    
    public void handleServerError(Throwable t){
    	t.printStackTrace();
        //Window.alert("Server error: " + t.toString());
    }
    
    private ListDataWidget createWidget(String aToken){
    	
    	ListDataWidget widget = null;
    	if(aToken.equals(TokenData.PERSON)){
    		widget = new ListPersonsWidget(this, null);
    	}
    	else if(aToken.equals(TokenData.TEAM)){
    		widget = new ListTeamsWidget(this, "", null);
    	}
    	else if(aToken.equals(TokenData.SERIE)){
    		widget = new ListSeriesWidget(this, SessionData.sCurrentTeam.getSeriesInfo());
    	}
    	else if(aToken.equals(TokenData.PLAYER)){
    		widget = new ListPlayersWidget(this, null);
    	}
    	else if(aToken.equals(TokenData.MATCH)){
    		widget = new ListMatchesWidget(this, null);
    	}
    	else
    		Window.alert("Unknown widget token: " + aToken);
    	return widget;
    	
    	
    }

	@Override
	public void forwardToken(String aWidget, String aData) {
		
		ListDataWidget widget = iTokenMap.get(aWidget);
		
		if(widget == null){
			widget = createWidget(aWidget);
			iTokenMap.put(aWidget, widget);
		}
		
		if(widget.needsServerUpdate()){
			widget.requestServerData(this);
		}
		
		setActiveWidget(widget);
		
		
	}

}

